package com.woniuxy.vuecli.controller;

import com.woniuxy.vuecli.entity.ResponseResult;
import com.woniuxy.vuecli.exception.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author author
 * @Create 2024-01-17 10:27
 */
//AOP:目标对象，代理对象，织入，切面，切入点，通知（前置，后置，环绕，异常，最终），顾问
@Slf4j
@RestControllerAdvice
public class GlobalException {

//    @ExceptionHandler(Exception.class)
//    public ResponseResult doException(Exception e) {
//        log.info("ex={}", e.getStackTrace());
//        log.info("ex={}", e.getClass());
//        log.info("ex={}", e.getMessage());
//        return new ResponseResult(500, "系统维护中，稍后再试", null);
//    }

    @ExceptionHandler(MneuReferenceExcetion.class)
    public ResponseResult doMneuReferenceExcetion(MneuReferenceExcetion e) {
        log.info("ex={}", e.getStackTrace());
        log.info("ex={}", e.getClass());
        log.info("ex={}", e.getMessage());
        return new ResponseResult(601, e.getMessage(), null);
    }
    @ExceptionHandler(VeriyCodeException.class)
    public ResponseResult doVeriyCodeException(VeriyCodeException e) {
        log.info("ex={}", e.getStackTrace());
        log.info("ex={}", e.getClass());
        log.info("ex={}", e.getMessage());
        return new ResponseResult(203, e.getMessage(), null);
    }

    @ExceptionHandler(PasswordWrongException.class)
    public ResponseResult doPasswordWrongException(PasswordWrongException e) {
        log.info("ex={}", e.getStackTrace());
        log.info("ex={}", e.getClass());
        log.info("ex={}", e.getMessage());
        return new ResponseResult(202, e.getMessage(), null);
    }
    @ExceptionHandler(EnameNotExistsException.class)
    public ResponseResult doEnameNotExistsException(EnameNotExistsException e) {
        log.info("ex={}", e.getStackTrace());
        log.info("ex={}", e.getClass());
        log.info("ex={}", e.getMessage());
        return new ResponseResult(201,  e.getMessage(), null);
    }
    @ExceptionHandler(EmailUsedException.class)
    public ResponseResult doException(EmailUsedException e) {
        log.info("ex={}", e.getStackTrace());
        log.info("ex={}", e.getClass());
        log.info("ex={}", e.getMessage());
        return new ResponseResult(101, "email-error", e.getMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult doMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.info("ex={}", e.getStackTrace());
        log.info("ex={}", e.getClass());
        log.info("ex={}", e.getMessage());
        BindingResult bindingResult = e.getBindingResult();
        Map<String, String> mapErrors = new HashMap<>();
        bindingResult.getFieldErrors()
                .forEach(item -> mapErrors.put(item.getField(), item.getDefaultMessage()));
        return new ResponseResult(100, "参数校验失败", mapErrors);

    }
}
